#!/usr/bin/python
# -*- coding: latin-1 -*-
# Copyright © 2008  Hedvig Kamp
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import GameManager
import ConsoleViewer
import Team
import Game
import Player
import sys

class ConsoleController:

    def __init__(self, game_mgr, console_viewer, stdin=sys.stdin, stdout=sys.stdout):
        self.cc_in = stdin
        self.cc_out = stdout
        self.gm = game_mgr
        self.cv = console_viewer
        self.game = Game
        self.team1 = Team
        self.team2 = Team
        self.teams = [self.team1, self.team2]
        self.run()

    def init_game_data(self):
        return

    def read_game_data(self):
        return

    def print_info(self, info):
        self.cc_out.write(str(info))
    
    def run(self):
        while True:
            self.cc_out.write(">> ")
            self.cc_out.flush()

            line = self.cc_in.readline().strip()
            if line == "exit":
                sys.exit()
            elif line == "help" or line == "h":
                self.print_info("To start a new game, use command 'newgame'")
            elif line == "":
                self.cc_out.write("\n>> ")
                self.cc_out.flush()
            elif line == "newgame" or line == "ng":
                x = 0
                ends = self.ask("\nNumber of ends:")
                #no_teams = self.ask("\nNumber of teams to score: ")
                while x < 2:
                    self.teams[x] = self.add_team(x+1)
                    x = x+1
                game = Game.Game(self.team1, self.team2, ends)
            elif line == "showteam" or line == "st":
                t = self.ask("\nTeam 1 or team 2? ")
                if t == "1" or t == "2":
                    self.cv.print_team_info(self.teams[int(t)-1])
                else:
                    self.print_info("\nPlease answer '1' or '2'") 
            elif line == "startgame":
                self.cv.print_info("\n**Game starting**")
                print "Game starting"
                curr_end = 1
                curr_player = 1
                curr_team = 1
                curr_stone = 1
                while int(curr_end) < int(game.get_no_ends()) + 1:
                    self.cv.print_info("End " + str(curr_end) + " starting")
                    no_players = self.teams[int(curr_team)].get_no_players()
                    while int(curr_player) < no_players + 1:
                        self.cv.print_info("\nPlayer "+str(curr_player)+" in team " + str(int(curr_team)) + " is playing stone number " + str(curr_stone))
                        self.cv.print_info("Score: ")
                        score = self.cc_in.readline().strip()
                        old_score = self.teams[int(curr_team)].get_player(curr_player).get_score()
                        stones_played = self.teams[curr_team].get_player(curr_player).get_stones_played()
                        stones_played = stones_played + 1
                        self.teams[curr_team].get_player(curr_player).set_stones_played(int(stones_played))
                        new_score = self.gm.calc_score( int(old_score), int(score), int(stones_played))
                        self.teams[int(curr_team)].get_player(curr_player).update_total_score(new_score)
                        curr_stone = curr_stone + 1
                        if curr_team == 1:
                            curr_team = 2
                        else:
                            curr_team = 1
                            curr_player = curr_player + 1
                        old_score = 0
                        stones_played = 0
                    curr_end = curr_end + 1
            elif line == "addteam" or line == "at":
                i = self.ask("\nTeam 1 or 2? ")
                self.add_team(int(i))
            else:
                self.print_info("Use command 'help' for help")
                
    def add_team(self, no):
        teamIdx = int(no) - 1
        self.cv.print_info( "\n**Team "+str(no)+"**" )
        name = self.ask("\nTeam name: ")
        no_players = self.ask("\nNumber of players in team: ")
        self.teams[teamIdx] = Team.Team(int(no_players), name)
        self.print_info("\n**Add team players**")
        players = 0
        while int(players) < int(no_players):
            self.cv.print_info("\n**Player "+str(players+1)+"**" )
            name = self.ask("\nName: ")
            pos = self.ask("\nPosition: ")
            no_stones = self.ask("\nNumber of stones per end: ")
            hand = self.ask("\nLeft or right handed player? ")
            player = Player.Player(name, no_stones, hand )
            players = players + 1
            self.teams[teamIdx].add_player(int(pos), player)
        return self.teams[teamIdx]

    def ask(self, question):
        self.cv.print_info(question)
        self.cc_out.write(">> ")
        return self.cc_in.readline().strip()
